<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Category extends MY_Luckydog
{
    public function __construct()
    {
        parent::__construct();
      /*  $func = $this->router->fetch_method();
        if($func != "upload_cover"){
            $login_status = $this->check_login_status();
            if ($login_status === FALSE){
                header("Location:login");
            }
        }*/
        $this->load->model('luckydog/Category_model', 'cat');
    }

/**
 * 新闻列表
 * @return [type] [description]
 */
    public function index(){
        $login_status = $this->check_login_status();
        $_SESSION['v_menu'] = 'category';
        $catList = $this->cat->getCatList();
        $data = array(
            'catList'=>$catList,
            );
        $this->load->view("/luckydog/category/index", $data);
    }

    public function textures()
    {
        $login_status = $this->check_login_status();
        $id = $this->input->get("id",true)?$this->input->get("id",true):"";
        $type = $this->input->get("type",true)?$this->input->get("type",true):0;
        if ($login_status === FALSE){
            header("Location:login");
        }
        $_SESSION['v_menu'] = 'category';
        $data = array();
        if(!empty($id)){
            $CatInfo = $this->cat->getCatInfobyID($id);
            $data["CatInfo"] = $CatInfo;
        }
        $data['type'] = $type;
        $this->load->view("/luckydog/category/textures", $data);
    }

    //编辑广告
    public function editCat(){
        $id = $this->input->post("id",true) ? $this->input->post("id",true) : 0;
        $imgurl = $this->input->post("cover",true) ? $this->input->post("cover",true) : "";
        $name = $this->input->post("name",true) ? $this->input->post("name",true) : "";
        $name_en = $this->input->post("name_en",true) ? $this->input->post("name_en",true) : "";
        $sort_order = $this->input->post("sort_order",true) ? $this->input->post("sort_order",true) : "";
        $data = array(
            "imgurl"=>$imgurl,
            "name"=>$name,
            "name_en"=>$name_en,
            "sort_order"=>$sort_order,
        );
        if(empty($id)){
            $data['addtime']=time();
        }
        $res = $this->cat->editCat($id,$data);
        echo $res;
    }

//编辑上线下，删除状态
    public function editStatus(){
        $id = $this->input->post("id",true) ? $this->input->post("id",true) : 0;
        $onlinestatus = $this->input->post("onlinestatus",true) ? $this->input->post("onlinestatus",true) : "";
        $type = $this->input->post("type",true) ? $this->input->post("type",true) : 0;
        $data = array(
            "id"=>$id,
            "onlinestatus"=>$onlinestatus,
            "type"=>$type
        );
        $res =$this->cat->editStatus($data);
        echo $res;
    }

    public function upload_cover()
    {
        $this->load->library('Upload');
        $upload = new Upload();
        $file_name =$upload->execute($_FILES['Filedata']);

        $savepath='./upload/ad/'.date('Y-m-d').'/';
        $upload->set_upload_dir($savepath);
        if (!file_exists($savepath)){
            mkdir($savepath);
        }
        if(is_string($file_name))
        {
            $res=json_encode(array('code'=>1,'type'=>0,'file_name'=>$file_name));

            echo $res;
        }
        else
        {
            echo json_encode(array('code'=>-1));
        }
    }

    //删除状态
    public function delCat(){
        $id = $this->input->post("id",true) ? $this->input->post("id",true) : 0;
        $res =$this->cat->delCat($id);
        echo $res;
    }


}
/* End of file index.php */
/* Location: ./application/controllers/luckydog/index.php */